import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//-------------------------
//  Definisi class Kanvas
//-------------------------
class Kanvas extends Canvas
{
   private int x;
   private int y;

   //-----------------------------
   //  Inisialisasi class Kanvas
   //-----------------------------
   public Kanvas()
   {
      x = 10;
      y = 30;

      setBackground (Color.white);
   }

   //---------------------------
   //  Menggambar objek grafis
   //---------------------------
	public void paint (Graphics g)
	{
      g.setColor (new Color (0,0,255,50));
      g.fillOval (x, y, 30, 30);

      g.setColor (Color.blue);
      g.drawOval (x, y, 30, 30);
	}

   //--------------------------
   //  Manipulasi data lokasi
   //--------------------------
   public void ubahX (int xx)
   {
      x = xx;
   }

   public int nilaiX()
   {
      return (x);
   }
}

//-----------------------------
//  Definisi class FrameUtama
//-----------------------------
class FrameUtama extends JFrame implements ActionListener
{
	private Kanvas kanvas = new Kanvas();

   private JPanel panelTombol = new JPanel();
   private Button tombolMulai = new Button ("Mulai");
   private Button tombolSelesai = new Button ("Selesai");
   private Button tombolExit = new Button ("EXIT");

   private JLabel indikatorArah = new JLabel (">>>");

   private boolean jalankan = false;
   private int arahBenda = 1;

	FrameUtama()
	{
		super ("Animasi Lingkaran");
		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		setLocation (100,100);
		setSize (300,160);

      tombolMulai.addActionListener (this);
      tombolSelesai.addActionListener (this);
      tombolExit.addActionListener (this);

      indikatorArah.setFont (new Font ("Arial Narrow", Font.BOLD, 15));
      indikatorArah.setHorizontalAlignment (JLabel.CENTER);

      panelTombol.setLayout (new GridLayout (1,3));
      panelTombol.add (tombolMulai);
      panelTombol.add (tombolSelesai);
      panelTombol.add (tombolExit);

		getContentPane().setLayout (new BorderLayout());
		getContentPane().add (kanvas, BorderLayout.CENTER);
      getContentPane().add (panelTombol, BorderLayout.SOUTH);
      getContentPane().add (indikatorArah, BorderLayout.NORTH);

		show();

      while (true)
      {
         if (jalankan)
         {
            if (kanvas.nilaiX() >= kanvas.getWidth()-30)
               arahBenda = -1;
            else
            if (kanvas.nilaiX() <= 0)
               arahBenda = 1;

            if (arahBenda == 1)
            {
               kanvas.ubahX (kanvas.nilaiX() + 5);

               indikatorArah.setText (">>>");
            }
            else
            {
               kanvas.ubahX (kanvas.nilaiX() - 5);

               indikatorArah.setText ("<<<");
            }
            
            kanvas.repaint();

            try
            {
               Thread.sleep (50);
            }
            catch (Exception e)
            {
            }
         }
      }
	}

   //------------------------------
   //  Implementasi event handler
   //------------------------------
   public void actionPerformed (ActionEvent e)
   {
      Object objek = e.getSource();

      if (objek == tombolMulai)
         jalankan = true;
      else
      if (objek == tombolSelesai)
         jalankan = false;
      else
      if (objek == tombolExit)
         System.exit (0);
   }
}

//---------------
//  class utama
//---------------
public class Animasi
{
	public static void main (String[] args) 
	{
		new FrameUtama();
	}
}